# Nginx

Nginx (opens new window) 是一个高性能的 HTTP 和反向代理服务器,广泛用于 Web 服务器、负载均衡、反向代理和 HTTP 缓存等多种场景。

# 一、安装

  • CentOS

    rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    yum info nginx
    yum install nginx
    

# 二、模块

# ngx_http_core_module

嵌入式变量:

  • $http (opens new window)_name:任意请求头字段;变量名的最后一部分是转换为小写的字段名,用下划线替换短划线。例如,$http_host 代表 host 请求头。

# ngx_http_ssl_module

ngx_http_ssl_module (opens new window) 模块为 HTTPS 提供了必要的支持。

此模块不是默认构建的,应使用 --with-http_ssl_module 配置参数启用。

http {
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    server {
        listen              443 ssl;
        server_name         www.example.com;
        
        keepalive_timeout   70;
        ssl_certificate     www.example.com.crt;
        ssl_certificate_key www.example.com.key;
        
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
    }
}

# ngx_http_proxy_module

ngx_http_proxy_module (opens new window) 模块允许将请求传递到另一台服务器。

location / {
    proxy_pass https://1.0.0.1;
    proxy_set_header Host $http_host;

    proxy_ssl_name $http_host;
    proxy_ssl_server_name on;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

如果 proxy_pass 指令是用 URI 指定的,那么当请求传递到服务器时,请求 URI 中与 location 匹配的部分将被 proxy_pass 指令中指定的 URI 替换。

# http://www.wandouduoduo.com/wddd/index.html

# http://127.0.0.1:8080/index.html
location /wddd/ {
    proxy_pass http://127.0.0.1:8080/;
}

# http://127.0.0.1:8080/sun/index.html
location /wddd/ {
    proxy_pass http://127.0.0.1:8080/sun/;
}

# http://127.0.0.1:8080/sunindex.html
location /wddd/ {
    proxy_pass http://127.0.0.1:8080/sun;
}

如果指定 proxy_pass 时没有 URI,则在处理原始请求时,请求 URI 将以客户端发送的相同形式传递给服务器。

# http://www.wandouduoduo.com/wddd/index.html

# http://127.0.0.1:8080/wddd/index.html
location /wddd/ {
    proxy_pass http://127.0.0.1:8080;
}